﻿using System;
using Informant.SmsMessaging;

namespace Informant.Clients.DataProviders.Wpf4.InformantService
{
	public class InformantServiceDataProvider : IInformantServiceDataProvider
	{
		private static readonly InformantServiceContext Context;

		static InformantServiceDataProvider()
		{
			Context = new InformantServiceContext();
		}

		#region Events

		public event EventHandler<LogOnCompletedEventArgs> LogOnAsyncCompleted;
		private void OnLogOnAsyncCompleted(LogOnCompletedEventArgs e)
		{
			var handler = LogOnAsyncCompleted;
			if (handler != null)
			{
				handler(this, e);
			}
		}
	
		public event EventHandler<LogOffCompletedEventArgs> LogOffAsyncCompleted;
		private void OnLogOffAsyncCompleted(LogOffCompletedEventArgs e)
		{
			var handler = LogOffAsyncCompleted;
			if (handler != null)
			{
				handler(this, e);
			}
		}
		
		public event EventHandler<RetrieveContactsEventArgs> RetrieveContactsAsyncCompleted;
		private void OnRetrieveContactsAsyncCompleted(RetrieveContactsEventArgs e)
		{
			var handler = RetrieveContactsAsyncCompleted;
			if (handler != null)
			{
				handler(this, e);
			}
		}
		
		public event EventHandler<RetrieveGroupsEventArgs> RetrieveGroupsAsyncCompleted;
		private void OnRetrieveGroupsAsyncCompleted(RetrieveGroupsEventArgs e)
		{
			var handler = RetrieveGroupsAsyncCompleted;
			if (handler != null)
			{
				handler(this, e);
			}
		}
		
		public event EventHandler<SendSmsProgressReportedEventArgs> SendSmsAsyncProgressReported;
		private void OnSendSmsAsyncProgressReported(SendSmsProgressReportedEventArgs e)
		{
			var handler = SendSmsAsyncProgressReported;
			if (handler != null)
			{
				handler(this, e);
			}
		}
		
		public event EventHandler<SendSmsCompletedEventArgs> SendSmsAsyncCompleted;
		private void OnSendSmsAsyncCompleted(SendSmsCompletedEventArgs e)
		{
			var handler = SendSmsAsyncCompleted;
			if (handler != null)
			{
				handler(this, e);
			}
		}

		#endregion

		#region Public Methods

		public void LogUserOnAsync(String username, String password)
		{
		    Context.LogOnAsyncCompleted += ContextLogOnAsyncCompletedHandler;
		    Context.LogOnAsync(username, password);
		}
		public void LogUserOffAsync()
		{
			Context.LogOffAsyncCompleted += ContextLogOffAsyncCompletedHandler;
			Context.LogOffAsync();
		}
		public void DeleteSmsAsync()
		{
			throw new NotImplementedException();
		}
		public void MarkReadSmsAsync(String smsId, Boolean isRead)
		{
			throw new NotImplementedException();
		}
		public void RetrieveAllSmsAsync()
		{
			throw new NotImplementedException();
		}
		public void SendSmsAsync(ISms sms)
		{
			var serviceSms = sms as Sms;
			if (serviceSms != null)
			{
				Context.SendSmsAsyncCompleted += ContextSendSmsAsyncCompletedHandler;
				Context.SendSmsAsyncProgressReported += ContextSendSmsAsyncProgressReportedHandler;
				Context.SendSmsAsync(serviceSms);
			}
			else
			{
				//TODO: Send a string reason why this failed.
				OnSendSmsAsyncCompleted(new SendSmsCompletedEventArgs(false, null));
			}
		}
		public void RetrieveContactsAsync()
		{
			Context.RetrieveContactsAsyncCompleted += ContextRetrieveContactsAsyncCompletedHandler;
			Context.RetrieveContactsAsync();
		}
		public void RetrieveGroupsAsync()
		{
			Context.RetrieveGroupsAsyncCompleted += ContextRetrieveGroupsAsyncCompletedHandler;
			Context.RetrieveGroupsAsync();
		}

		#endregion

		#region Private Methods

		void IContactsDataProvider.LogOnAsync(string username, string password)
		{
			LogUserOnAsync(username, password);
		}
		void ISmsDataProvider.LogOffAsync()
		{
			LogUserOffAsync();
		}
		void IContactsDataProvider.LogOffAsync()
		{
			LogUserOffAsync();
		}
		void ISmsDataProvider.LogOnAsync(string username, string password)
		{
			LogUserOnAsync(username, password);
		}
		void ContextLogOnAsyncCompletedHandler(object sender, LogOnCompletedEventArgs e)
		{
			OnLogOnAsyncCompleted(e);
		}
		void ContextLogOffAsyncCompletedHandler(object sender, LogOffCompletedEventArgs e)
		{
			OnLogOffAsyncCompleted(e);
		}
		void ContextSendSmsAsyncCompletedHandler(object sender, SendSmsCompletedEventArgs e)
		{
			OnSendSmsAsyncCompleted(e);
		}
		void ContextSendSmsAsyncProgressReportedHandler(object sender, SendSmsProgressReportedEventArgs e)
		{
			OnSendSmsAsyncProgressReported(e);
		}
		void ContextRetrieveContactsAsyncCompletedHandler(object sender, RetrieveContactsEventArgs e)
		{
			OnRetrieveContactsAsyncCompleted(e);
		}
		void ContextRetrieveGroupsAsyncCompletedHandler(object sender, RetrieveGroupsEventArgs e)
		{
			OnRetrieveGroupsAsyncCompleted(e);
		}

		#endregion
	}
}
